package com.anpy.management.controller;

import com.anpy.common.service.BaseController;
import com.anpy.common.service.model.vo.ResponseMessage;
import com.anpy.common.service.model.vo.Result;
import com.anpy.management.entity.TaskEntity;
import com.anpy.management.service.TaskService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 任务管理控制器
 */
@RestController
@RequestMapping("/api/task")
@Api(tags = "任务管理接口")
@ApiImplicitParams({
   @ApiImplicitParam(paramType = "header", name = "Authorization", value = "用户认证token", dataType = "String", required = true, example = "Bearer token"),
})
public class TaskController extends BaseController {

    @Resource
    private TaskService taskService;

    public TaskController(HttpServletRequest request) {
        super(request);
    }

    /**
     * 创建任务
     */
    @PostMapping("/create")
    @ApiOperation("创建任务")
    public ResponseMessage<Boolean> createTask(@RequestBody @ApiParam("任务信息") TaskEntity taskEntity) {
        taskEntity.setUserId(getUserId());
        return Result.success(taskService.createTask(taskEntity));
    }

    /**
     * 更新任务
     */
    @PutMapping("/update")
    @ApiOperation("更新任务")
    public ResponseMessage<Boolean> updateTask(@RequestBody @ApiParam("任务信息") TaskEntity taskEntity) {
        return Result.success(taskService.updateTask(taskEntity));
    }

    /**
     * 删除任务
     */
    @DeleteMapping("/delete/{taskId}")
    @ApiOperation("删除任务")
    public ResponseMessage<Boolean> deleteTask(@PathVariable("taskId") @ApiParam("任务ID") Integer taskId) {
        return Result.success(taskService.deleteTask(taskId));
    }

    /**
     * 获取任务详情
     */
    @GetMapping("/get/{taskId}")
    @ApiOperation("获取任务详情")
    public ResponseMessage<TaskEntity> getTask(@PathVariable("taskId") @ApiParam("任务ID") Integer taskId) {
        return Result.success(taskService.getTask(taskId));
    }

    /**
     * 获取用户的所有任务
     */
    @GetMapping("/list")
    @ApiOperation("获取用户的所有任务")
    public ResponseMessage<List<TaskEntity>> getUserTasks() {
        Long userId = getUserId();
        return Result.success(taskService.getUserTasks(userId));
    }

    /**
     * 获取项目的所有任务
     */
    @GetMapping("/project/{projectId}")
    @ApiOperation("获取项目的所有任务")
    public ResponseMessage<List<TaskEntity>> getProjectTasks(@PathVariable("projectId") @ApiParam("项目ID") Integer projectId) {
        return Result.success(taskService.getProjectTasks(projectId));
    }

    /**
     * 分页查询用户任务
     */
    @GetMapping("/page")
    @ApiOperation("分页查询用户任务")
    public ResponseMessage<Page<TaskEntity>> getUserTasksByPage(
            @RequestParam(defaultValue = "1") @ApiParam("页码") int page,
            @RequestParam(defaultValue = "10") @ApiParam("每页大小") int size,
            @RequestParam(required = false) @ApiParam("项目ID") Integer projectId,
            @RequestParam(required = false) @ApiParam("当前状态") String currentStatus,
            @RequestParam(required = false) @ApiParam("任务名称") String taskName) {
        Long userId = getUserId();
        return Result.success(taskService.getUserTasksByPage(page, size, userId, 
                projectId, currentStatus, taskName));
    }
}
